/**
 * @file 基础工具
 * @author its-wild(https://gitee.com/its-wild)
 * @date 2021/07/13
 */

import { JSEncrypt } from 'jsencrypt'

/**
 * 判断是否为undefined、null、''
 * @param val 要判断的值
 * @returns boolean
 */
export const isUndefinedEmptyNull = (val: unknown): boolean => {
  return val === undefined || val === null || val === ''
}

/**
 * 字符串 RSA 加密
 * @param publicKey 公钥
 * @param str 要加密的字符串
 * @returns 加密后的 base64 字符串 | false
 */
export const encryptStr = (publicKey: string, str: string): string | false => {
  const encrypt = new JSEncrypt()
  // 设置公钥
  encrypt.setPublicKey(publicKey)
  return encrypt.encrypt(str)
}

/**
 * 字符串 RSA 解密
 * @param privateKey 私钥
 * @param str 要解密的 base64 字符串
 * @returns 解密后的字符串 | false
 */
export const decryptStr = (privateKey: string, str: string): string | false => {
  const decrypt = new JSEncrypt()
  // 设置私钥
  decrypt.setPrivateKey(privateKey)
  return decrypt.decrypt(str)
}
